package com.xxx.blog.controller;

import com.github.pagehelper.PageInfo;
import com.xxx.base.utils.R;
import com.xxx.base.vo.GdVo;
import com.xxx.blog.domain.Article;
import com.xxx.blog.domain.ArticleType;
import com.xxx.blog.query.ArticleQuery;
import com.xxx.blog.service.IArticleService;
import com.xxx.blog.service.IArticleTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/article")
@Api(tags = "article接口")
public class ArticleController {

    @Autowired
    IArticleService articleService;

    @GetMapping
    @ApiOperation(value = "分页所有文章")
    public R<List<Article>> findAll(){
        List<Article> articles = articleService.findAll();
        return R.ok(articles);
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询")
    public R<Article> findOne(@PathVariable long id){
        Article one = articleService.findOne(id);
        return R.ok(one);
    }

    @PostMapping
    @ApiOperation(value = "分页加条件查询")
    public PageInfo<Article> queryPage(@RequestBody ArticleQuery articleQuery){
        List<Article> list = articleService.queryPage(articleQuery);
        return new PageInfo<Article>(list);
    }

    @PutMapping
    @ApiOperation(value = "查询新增")
    public R<Void> updateArticle(@RequestBody Article article){
        Long id = article.getId();
        try {
            if(id==null){
                articleService.addArticle(article);
            }else {
            articleService.updateArticle(article);}
        } catch (Exception e) {
            e.printStackTrace();
           return R.fail();
        }
        return R.ok();
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除某个")
    public R<Void> deleteArticle(@PathVariable long id){
        try {
            articleService.deleteArticle(id);
        } catch (Exception e) {
            e.printStackTrace();
            R.fail();
        }
        return R.ok();
    }

    @PatchMapping
    @ApiOperation(value = "批量删除")
    public R<Void> batch(@RequestBody Long[] ids){
        try {
            articleService.batchArticle(ids);
        } catch (Exception e) {
            e.printStackTrace();
            R.fail();
        }
        return R.ok();
    }

    @GetMapping("/gd")
    @ApiOperation(value = "归档查询")
    public List<GdVo> getGd(){
        List<GdVo> res = articleService.getGd();
        return res;
    }
    @GetMapping("/ra/{id}")
    @ApiOperation(value = "根据标签")
    public List<Article> getRa(@PathVariable long id){
        List<Article> ra = articleService.getRa(id);
        return ra;
    }


}
